namespace XEAS.Framework.Security
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class StringEncryptor
    {
        public static byte[] Decrypt(byte[] encryptedData, string Password)
        {
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0x76 });
            return Decrypt(encryptedData, bytes.GetBytes(0x20), bytes.GetBytes(0x10));
        }

        public static string Decrypt(string cipherText, string Password)
        {
            byte[] encryptedData = Convert.FromBase64String(cipherText);
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0x76 });
            byte[] buffer2 = Decrypt(encryptedData, bytes.GetBytes(0x20), bytes.GetBytes(0x10));
            return Encoding.Unicode.GetString(buffer2);
        }

        private static byte[] Decrypt(byte[] encryptedData, byte[] Key, byte[] IV)
        {
            MemoryStream stream = new MemoryStream();
            Rijndael rijndael = Rijndael.Create();
            rijndael.Key = Key;
            rijndael.IV = IV;
            CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(encryptedData, 0, encryptedData.Length);
            stream2.Close();
            return stream.ToArray();
        }

        public static string Encrypt(string clearText, string Password)
        {
            byte[] clearData = Encoding.Unicode.GetBytes(clearText);
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0x76 });
            return Convert.ToBase64String(Encrypt(clearData, bytes.GetBytes(0x20), bytes.GetBytes(0x10)));
        }

        public static byte[] Encrypt(byte[] clearData, string Password)
        {
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0x76 });
            return Encrypt(clearData, bytes.GetBytes(0x20), bytes.GetBytes(0x10));
        }

        private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream stream = new MemoryStream();
            Rijndael rijndael = Rijndael.Create();
            rijndael.Key = Key;
            rijndael.IV = IV;
            CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(clearData, 0, clearData.Length);
            stream2.Close();
            return stream.ToArray();
        }

        public static string MD5Encrypt(string originalText)
        {
            MD5 md = MD5.Create();
            Encoding encoding = Encoding.UTF8;
            byte[] bytes = encoding.GetBytes(originalText);
            byte[] buffer2 = md.ComputeHash(bytes, 0, bytes.Length);
            return encoding.GetString(buffer2, 0, buffer2.Length);
        }

        public static string SHA256Encrypt(string originalText)
        {
            SHA256Managed managed = new SHA256Managed();
            Encoding encoding = Encoding.UTF8;
            byte[] bytes = encoding.GetBytes(originalText);
            byte[] buffer2 = managed.ComputeHash(bytes, 0, bytes.Length);
            return encoding.GetString(buffer2, 0, buffer2.Length);
        }

        public static string SHA512Encrypt(string originalText)
        {
            SHA512Managed managed = new SHA512Managed();
            Encoding encoding = Encoding.UTF8;
            byte[] bytes = encoding.GetBytes(originalText);
            byte[] buffer2 = managed.ComputeHash(bytes, 0, bytes.Length);
            return encoding.GetString(buffer2, 0, buffer2.Length);
        }
    }
}

